package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.Roleservice;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/role")
@Slf4j
public class Rolecontroller {
   @Reference
   Roleservice roleservice;
//增
    @RequestMapping("/add")
    public Result getMemberReport(@RequestBody Map<String,Object>map){
              Object o= map.get("Role");
              JSONObject jsonObject =(JSONObject) o;
              Role role =jsonObject.toJavaObject(Role.class);

           Object o1= map.get("pids");
           JSONArray jsonObject1 = (JSONArray) o1;
         Integer[] pids = jsonObject1.toArray(new Integer[]{});

        Object o2= map.get("mids");
        JSONArray jsonObject2 = (JSONArray) o2;
        Integer[] mids = jsonObject2.toArray(new Integer[]{});
        log.debug("role+++++++++++++++++++++++",role);
        roleservice.add( mids,  pids,role);
        return new Result(true, MessageConst.ADD_Role_SUCCESS);
    }

//分页
@RequestMapping("/findPage")
public Result findByPage(@RequestBody QueryPageBean queryPageBean){
    PageResult pageResult = roleservice.findByPage(queryPageBean);
    log.debug("分页成功!!");
    return new Result(true, MessageConst.QUERY_RoleF_SUCCESS, pageResult);
}
//删
@RequestMapping("/delById")
public Result delById(Integer id){
roleservice.delById(id);
    log.debug("删除成功！！！");
    return new Result(true,MessageConst.DElecT_Role_SUCCESS);
}


//查权限选项回显
@RequestMapping("/findAllp")
public Result findAllp(){
    List<Permission> p = roleservice.findAllp();
    log.debug("查询权限选项成功！！！");
    return new Result(true,MessageConst.QUERY_Role_SUCCESS,p);
}
//查菜单选项回显
@RequestMapping("/findAllm")
    public Result findAllm(){
        List<Menu> m = roleservice.findAllm();
        log.debug("查询菜单选项成功！！！");
        return new Result(true,MessageConst.QUERY_Role_SUCCESS,m);
    }
    //改
    @RequestMapping("/findmById")
    public Result findmById(Integer id){
        List<Integer> mids = roleservice.findmById(id);
        return new Result(true, MessageConst.QUERY_Role_SUCCESS,mids);
    }
    @RequestMapping("/findpById")
    public Result findpById(Integer id){
        List<Integer> mids = roleservice.findpById(id);
        return new Result(true, MessageConst.QUERY_Role_SUCCESS,mids);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Role role = roleservice.findById(id);
        return new Result(true, MessageConst.QUERY_Role_SUCCESS,role);
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object>map){
        Object o= map.get("Role");
        JSONObject jsonObject =(JSONObject) o;
        Role role =jsonObject.toJavaObject(Role.class);
        Object o1= map.get("pids");
        JSONArray jsonObject1 = (JSONArray) o1;
        Integer[] pids = jsonObject1.toArray(new Integer[]{});
        Object o2= map.get("mids");
        JSONArray jsonObject2 = (JSONArray) o2;
        Integer[] mids = jsonObject2.toArray(new Integer[]{});
        roleservice.edit( mids,pids,role);
        return new Result(true, MessageConst.Edit_Role_SUCCESS,role);
    }


    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("findAll:");
        List<Role>roleList= roleservice.findAll();
        log.debug("查询所有角色成功!!");
        return new Result(true, "查询所有角色成功!!",roleList);
    }

}